console.log("运行必应搜索");
const date = new Date();
const today = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
const hour = date.getHours() + ":" + date.getMinutes();

window.onload = function () {

    //搜索模式
    chrome.storage.local.get(['manualSearch', 'searchDate', 'limitedDate', 'isAutoSignIn', 'isLock', 'executionTime', 'entry', 'progress'], function (results) {


        if (results.isAutoSignIn === "1" || results.manualSearch === "1") { // 有自动化 或手动 搜索时 就进来

             //判断当天是否进行签到
            //搜索的签到 如果当天没签到 但是 卡在了时间点 就进来
            if (results.searchDate !== today && convertToMinutes(results.executionTime) < convertToMinutes(hour)) {
                console.log("搜索模式：" + results.manualSearch + "|" + results.progress)
                switch (results.progress) {
                    case '2'://搜索触发
                        keywordSearch(results.entry);
                        break;
                    case '3'://全部任务触发
                        todayExchange();
                        break;
                    default:
                        window.location.href = "https://www.toutiao.com";
                        break;
                }
                return;
            }
            //判断当天是否进行限时签到
            if (results.limitedDate !== today && convertToMinutes("12:30") < convertToMinutes(hour) && results.isLock !== '1') {
                switch (results.progress) {
                    case '4'://限时签到触发
                        chrome.storage.local.set({isLock: "1"})
                        limitedExchange();
                        break;
                    default:
                        console.log('限时任务停止')
                        break;
                }
                return;
            }

        }
    })
}

function keywordSearch(entry) {
    let keyword = entry.shift();
    document.querySelector("body").insertAdjacentHTML("afterbegin", `<p style="position: fixed;top: 0;left: 0;z-index: 11111;background: white;color: black;">还剩词条：<b style="color: red;">${entry.length+1}</b> 个,下一个搜索关键词：${keyword}</p>`);
    chrome.storage.local.set({entry, progress: (entry.length === 0 ? '3' : '2')}, () => {
        setInterval(function () {
            window.scrollBy(0, getRandomInt(10,30))
        },getRandomInt(150,300))
        setTimeout(function () {
            document.getElementById("sb_form_q").value = keyword;
            document.getElementById("sb_form_go").click();
        }, getRandomInt(15000,30000));
    });
}

function todayExchange() {
    setTimeout(function () {
        document.querySelector(".b_clickarea").click();
    }, 500);
}

function limitedExchange() {
    setTimeout(function () {
        document.querySelector(".b_clickarea").click();
    }, 500);
}

function convertToMinutes(timeString) {
    const [hours, minutes] = (timeString ?? '').split(':').map(Number);
    const currentTime = (hours * 60 + minutes);
    return isNaN(currentTime) ? 0 : currentTime;
}

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min; // 包含min和max
}